use tokio::sync::{mpsc, oneshot};
use tracing::info;

use crate::network_core::P2pNetWorkError;

#[derive(Clone, Debug)]
pub(crate) struct NetWorkCoreInnerApi {
    pub(super) command_sender_for_network_core: mpsc::Sender<CommandOfNetWorkCore>,
}

impl NetWorkCoreInnerApi {
    pub(crate) async fn cleanup_swarm(&mut self) -> Result<(), P2pNetWorkError> {
        info!("Cleaning up swarm...");
        let (sender, receiver) = oneshot::channel();
        self.command_sender_for_network_core
            .send(CommandOfNetWorkCore::CleanupSwarm {
                cmd_ack_sender: sender,
            })
            .await
            .expect("Command receiver not to be dropped.");
        receiver.await.expect("Sender not to be dropped.")
    }
}

#[derive(Debug)]
pub(crate) enum CommandOfNetWorkCore {
    CleanupSwarm {
        cmd_ack_sender: oneshot::Sender<Result<(), P2pNetWorkError>>,
    },
}
